// pages/index/homePage/index.js
var bmap = require('../../../utils/bmap-wx.min.js')
import Common from '../../../utils/common.js'
let http = new Common()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    month: '',
    day: '',
    cityId:'',
    city: '成都市',
    latitude:'',
    longitude:'',
    headImg:''
  },
  userLocationShow: function () {
    let that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userLocation']) {
          wx.getLocation({
            success: function (res) {
              
              wx.setStorageSync('latitude', res.latitude)
              wx.setStorageSync('longitude', res.longitude)

              that.setData({
                latitude: res.latitude,
                longitude: res.longitude
              })
              that.getLocationData(res)
              // console.log(res)
            }
          })
        } else {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.getLocation({
                success: function (res) {
                  wx.setStorageSync('latitude', res.latitude)
                  wx.setStorageSync('longitude', res.longitude)
                  
                  that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                  })
                  that.getLocationData(res)
                }
              })
            }
          })
        }
      }
    })
  },
  getLocationData(res) {
    let that = this
    let BMap = new bmap.BMapWX({
      ak: 'gra5f4eUdz12CutGCGhgbm7sQ1wpPaGK'
    });

    let latitude = res.latitude //经度   
    let longitude = res.longitude //纬度   
    let speed = res.speed
    let accuracy = res.accuracy
    // console.log('aa')
    BMap.regeocoding({
      location: latitude + ',' + longitude,
      success: function (res) {
        console.log(res)
        that.setData({
          city: res.originalData.result.addressComponent.city
        })
      },
      fail: function (res) {
        // console.log(res)
      }
    })
  },
  loginShow: function () {
    let that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              // console.log(res)
              wx.setStorageSync('myInfo', res.rawData)
              that.getheadImg()
            }
          })
        } else {
          wx.login({
            success: function (res) {
              // console.log(wx.getStorageSync('code'))
              http.http({
                url: 'mp2/token/getToken',
                data: {
                  code: res.code
                },
                success(res) {
                  wx.setStorageSync('token', res.data.data.token)
                  that.getheadImg()
                  wx.navigateTo({
                    url: `/pages/index/login/my_login/index`,
                  })
                  
                }
              })
            }
          })

        }
      }
    })
  },
  MyCity: function () {
    wx.navigateTo({
      url: '/pages/city/index'
    })
  },
  MyDate: function () {
    wx.navigateTo({
      url: '/pages/calendar/index'
    })
  },
  orderClick: function () {
    let that = this
    http.http({
      url:'mp2/store/getStoresBycity',
      data:{
        cityname: that.data.city,
        lat: that.data.latitude,
        long: that.data.longitude
      },
      success(res){
        let data = JSON.stringify(res.data.data)
        wx.navigateTo({
          url: `/pages/index/lists/index?data=${data}`
        })
      }
    })
  },
  getDate: function () {
    let month
    let day
    let year

    let date = new Date();

    if (wx.getStorageSync('month')) {
      month = wx.getStorageSync('month') < 10 ? `0${wx.getStorageSync('month')}` : wx.getStorageSync('month') 
    } else {
      month = (date.getMonth() + 1) < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
    }

    if (wx.getStorageSync('Day')) {
     
      day = parseInt(wx.getStorageSync('Day')) < 10 ? `0${parseInt(wx.getStorageSync('Day'))}` : parseInt(wx.getStorageSync('Day')) 
    } else {
      day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
    }
    
    wx.setStorageSync('month', month)
    wx.setStorageSync('Day', day)
    
    this.setData({
      month: month,
      day: day,
    })
  },
  getheadImg:function(){
    let that = this
    http.http({
      url:'mp2/store/shouyeimg',
      success(res){
        that.setData({
          headImg: res.data.data.imgurl
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    

    this.loginShow()

    this.userLocationShow()

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDate()
    let city = wx.getStorageSync('city') || '成都市'
    this.setData({
      city: city,
    })
    // console.log(wx.getStorageSync('city'))
    // console.log(city)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.setStorageSync('month', )
    wx.setStorageSync('Day', )
    this.getDate()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})